package com.lcc.dingtalk.robot.base.bean;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.lcc.dingtalk.robot.base.enums.ENMsgCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.io.Serializable;

/**
 * @author lincc
 * @version 1.0  2018/11/27
 */
@Getter
@Setter
@NoArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResultContext<T> implements Serializable {

    private static final long serialVersionUID = -841950016937305243L;
    /**
     * 状态码，对应枚举ENMsgCode
     * 0：请求操作成功
     * 1：业务错误
     * 2：系统异常
     * 3：未登录
     * 4：session超时
     * 5：没有权限
     */
    private String code;

    /**
     * 提示信息
     */
    private String info;

    /**
     * 用于区分错误细节的值，比如登录失败的多种情况
     */
    private String value;

    /**
     * 成功标识
     */
    private Boolean success;

    /**
     * 需要返回的数据
     */
    private T data;

    public ResultContext(T data) {
        this();
        this.setData(data);
        this.setSuccess(true);
        this.setInfo(ENMsgCode.SUCCESS.getLabel());
        this.setCode(ENMsgCode.SUCCESS.getValue());
    }

    private ResultContext(String code, String info, String value, Boolean success) {
        this.code = code;
        this.info = info;
        this.value = value;
        this.success = success;
    }

    private ResultContext(String code, String info, Boolean success) {
        this.code = code;
        this.info = info;
        this.success = success;
    }

    private ResultContext(String code, String info, Boolean success, T data) {
        this.code = code;
        this.info = info;
        this.success = success;
        this.data = data;
    }

    public static <T> ResultContext<T> success(String info) {
        return new ResultContext<>(ENMsgCode.SUCCESS.getValue(), info, true);
    }

    public static <T> ResultContext<T> successData(T data) {
        return new ResultContext<>(ENMsgCode.SUCCESS.getValue(), ENMsgCode.SUCCESS.getLabel(), true, data);
    }

    public static <T> ResultContext<T> businessFail(String info) {
        return new ResultContext<>(ENMsgCode.BUSINESS_ERROR.getValue(), info, info, false);
    }

    public static <T> ResultContext<T> businessFail(String info, String value) {
        return new ResultContext<>(ENMsgCode.BUSINESS_ERROR.getValue(), info, value, false);
    }

    public static <T> ResultContext<T> systemException(String info, String value) {
        return new ResultContext<>(ENMsgCode.SYSTEM_EXCEPTION.getValue(), info, value, false);
    }
}
